<template>
    <el-dialog :title="`${ruleForm.id ? '编辑' : '新增'}仓库信息`" :visible.sync="dialogVisible" width="25%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto">
            <c-form-template lable="仓库信息">
                <el-col :span="24">
                    <el-form-item label="仓库名称" prop="name">
                        <el-input v-model="ruleForm.name" placeholder="请输入仓库名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="国家/地区" prop="addressCountry">
                        <el-select v-model="ruleForm.addressCountry" placeholder="请选择仓库所在的国家/地区" style="display:block" @change="countryChange">
                            <el-option :label="item.label" :value="item.value" v-for="(item,index) in countryList" v-bind:key="index"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="城市" prop="addressCity">
                        <el-select v-model="ruleForm.addressCity" placeholder="请先选择仓库所在的国家/地区" style="display:block">
                            <el-option :label="item.label" :value="item.value" v-for="(item,index) in cityList.children" v-bind:key="index"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="仓库地址" prop="addressDetails">
                        <el-input v-model="ruleForm.addressDetails" placeholder="请输入仓库详细地址"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="联系人" prop="contactPerson">
                        <el-input v-model="ruleForm.contactPerson" placeholder="请输入联系人"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="联系方式" prop="contactPhone">
                        <el-input v-model="ruleForm.contactPhone" placeholder="请输入联系方式"></el-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
            <c-form-template lable="费用配置">
                <el-col :span="24">
                    <el-form-item label="入库费" prop="feeIn">
                        <c-money-input v-model="ruleForm.feeIn" placeholder="请输入入库费"><template slot="append"> 元 </template></c-money-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="出库费" prop="feeOut">
                        <c-money-input v-model="ruleForm.feeOut" placeholder="请输入出库费"><template slot="append"> 元 </template></c-money-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="仓储费" prop="charge">
                        <c-money-input v-model="ruleForm.charge" placeholder="请输入仓储费"> <template slot="append">元/天</template></c-money-input>
                    </el-form-item>
                </el-col>
            </c-form-template>
        </el-form>
        <span slot="footer">
            <el-button :loading="loading" @click="handleClose">取 消</el-button>
            <el-button :loading="loading" type="primary" @click="handleSubmit">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import countryList from '../../../components/form/countryData.js'
import cityList from '../../../components/form/countryCityData.js'
export default {
    data () {
        return {
            dialogVisible: false,
            loading: false,
            countryList: countryList,
            cityList: [],
            ruleForm: {
                addressCity: ""
            },
            rules: {
                name: [{ required: true, message: "请输入仓库名称", trigger: "blur" }],
                addressCountry: [{ required: true, message: "请选择仓库所在的国家/地区", trigger: "blur" }],
                addressCity: [{ required: true, message: "请选择仓库所在的城市", trigger: "blur" }],
                addressDetails: [{ required: true, message: "请选择仓库详细地址", trigger: "blur" }],
                feeIn: [{ required: true, message: "请输入入库费", trigger: "blur" }],
                feeOut: [{ required: true, message: "请输入出库费", trigger: "blur" }],
                charge: [{ required: true, message: "请输入仓储费", trigger: "blur" }],
                contactPerson: [{ required: false, message: "请输入联系人", trigger: "blur" }],
                contactPhone: [{ required: false, message: "请输入联系方式", trigger: "blur" }],
            },
        };
    },
    methods: {
        countryChange (e) {
            let Index = 0
            this.countryList.forEach((item, index) => {
                if (e == item.value) {
                    Index = index
                }
            })
            this.cityList = cityList[Index]
            this.ruleForm.addressCity = ""
        },
        handleSubmit () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.$confirm("是否确认此操作?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        this.loading = true;
                        const data = JSON.parse(JSON.stringify(this.ruleForm));
                        this.$post({
                            url: `/StoreInfo/${data.id ? "update" : "insert"}`,
                            data,
                        }).then(
                            () => {
                                this.$message.success("操作成功");
                                this.handleClose();
                                this.$parent.$parent.tableList();
                                this.loading = false;
                            },
                            () => {
                                this.loading = false;
                            }
                        );
                    });
                }
            });
        },
        handleOpen (item) {
            if (item && item.id) {
                this.ruleForm = {
                    ...item,
                    address: [item.addressArea, item.addressDetails],
                };
            }
            this.dialogVisible = true;
            this.loading = false;
        },
        handleClose () {
            this.ruleForm = {
                address: [],
            };
            this.$refs.ruleForm.resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>
